﻿using System;
using System.Web;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.SUPPORT.Controls
{
    public partial class AddEditTopic : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            // Create topic.
            Components.Topic topic = new Components.Topic();
            topic.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue);
            topic.Scope = rbPublicScope.Checked ? "Public" : "Private";
            topic.Title = HttpUtility.HtmlEncode(txtTitle.Text);
            topic.Status = (int) TopicStatus.NewTopic;
            topic.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
            topic.CreatedDate = DateTime.Now;
            topic.Insert();
            
            // Create message.
            Components.Message message = new Components.Message();
            message.TopicID = topic.ID;
            message.Memo = HttpUtility.HtmlEncode(txtContent.Text);
            message.CreatedBy = topic.CreatedBy;
            message.CreatedDate = topic.CreatedDate;
            message.Insert();

            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Topic);
            Response.Redirect(url);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Topic);
            Response.Redirect(url);
        }
    }
}